package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.marketing.MarketingPdfCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingPdfUpdateDTO;
import com.pactera.madp.cp.api.entity.marketing.MarketingPdfEntity;
import com.pactera.madp.cp.api.vo.marketing.MarketingPdfVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.marketing.MarketingPdfMapper;
import com.pactera.madp.cp.service.MarketingHotService;
import com.pactera.madp.cp.service.MarketingPdfService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (McMarketingPdf)表服务实现类
 *
 * @author yushen.qi
 * @since 2021-05-24 18:43:42
 */
@Service
public class MarketingPdfServiceImpl extends ServiceImpl<MarketingPdfMapper, MarketingPdfEntity> implements MarketingPdfService {

    @Resource
    private MarketingHotService marketingHotService;

    @Override
    public List<MarketingPdfVO> selectAllOrName(String title, Integer classification) {
        int corpId = CorpContextHolder.getCorpId();
        if(null==title){
            return list(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getCorpId, corpId).eq(MarketingPdfEntity::getClassification,classification)).stream().map(entity ->
            {
                MarketingPdfVO MarketingPdfVO = new MarketingPdfVO();
                BeanUtil.copyProperties(entity, MarketingPdfVO);
                return MarketingPdfVO;
            }).collect(Collectors.toList());
        }
        return list(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getCorpId, corpId).like(MarketingPdfEntity::getTitle,title).eq(MarketingPdfEntity::getClassification,classification)).stream().map(entity ->
        {
            MarketingPdfVO MarketingPdfVO = new MarketingPdfVO();
            BeanUtil.copyProperties(entity, MarketingPdfVO);
            return MarketingPdfVO;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage selectAllByPageOrName(Page page, String title, Integer classification) {
        Integer corpId = CorpContextHolder.getCorpId();
        QueryWrapper<MarketingPdfEntity> wrapper = new QueryWrapper<>();
        if(null==title){
            wrapper.eq("corp_id",corpId).eq("marketing_state",2).eq("classification",classification);
        }else {
            wrapper.like("title", title).eq("marketing_state",2).eq("corp_id", corpId).eq("classification", classification);
        }
        IPage iPage = baseMapper.selectPage(page, wrapper);
        Object collect = iPage.getRecords().stream().map(entity -> {
            MarketingPdfVO marketingPdfVO = new MarketingPdfVO();
            BeanUtil.copyProperties(entity, marketingPdfVO);
            return marketingPdfVO;
        }).collect(Collectors.toList());
        iPage.setRecords((List) collect);
        return iPage;
    }

    @Override
    public Map<String, Integer> insert(MarketingPdfCreateDTO dto) {
        Integer corpId = CorpContextHolder.getCorpId();
        int tenantId = AccountService.getTenantId();
        MarketingPdfEntity pdfEntity = new MarketingPdfEntity();
        BeanUtil.copyProperties(dto,pdfEntity);
        pdfEntity.setCreateTime(LocalDateTime.now());
        pdfEntity.setUpdateTime(LocalDateTime.now());
        pdfEntity.setCorpId(corpId);
        pdfEntity.setHotUuid("a");
        pdfEntity.setRecState(2);
      //  pdfEntity.setTenantId(tenantId);
        pdfEntity.setMarketingState(1);
        pdfEntity.setMarketingCover("/mfs/pdf.png");
        save(pdfEntity);
        MarketingPdfEntity one = getOne(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getCorpId, corpId).orderByDesc(MarketingPdfEntity::getId).last("limit 1"));
        HashMap<String, Integer> hashMap = new HashMap<>(1);
        hashMap.put("id",one.getId());
        return hashMap;
    }

    @Override
    public boolean update(MarketingPdfUpdateDTO marketingPdfUpdateDto) {
        MarketingPdfEntity pdfEntity = new MarketingPdfEntity();
        BeanUtil.copyProperties(marketingPdfUpdateDto,pdfEntity);
        pdfEntity.setUpdateTime(LocalDateTime.now());
        pdfEntity.setMarketingState(3);
        pdfEntity.setHotUuid(null);
        pdfEntity.setRecState(2);
        return update(pdfEntity, Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId,pdfEntity.getId()));
    }

    @Override
    public R<Boolean> remove(Integer id) {
        MarketingPdfEntity one = getOne(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId, id));
        if (one.getMarketingState()==2){
            return R.failed("该素材已被发布，无法删除！");
        }
        if(one.getRecState()==1){
            return R.failed("该素材已被推荐，无法删除！");
        }

//        MarketingHotEntity hotEntity = marketingHotService.getOne(Wrappers.<MarketingHotEntity>lambdaQuery()
//                .eq(MarketingHotEntity::getCorpId,CorpContextHolder.getCorpId())
//                .orderByDesc(MarketingHotEntity::getId).last("limit 1"));
//        if(null!=hotEntity){
//            if(one.getHotUuid().equals(hotEntity.getHotUuid())){
//                return R.failed("该素材已在热榜发布，无法删除！");
//            }
//        }
        return remove(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getId,id))?R.ok():R.failed("删除失败");
    }


}
